package gr.unipi.mscd1;

import javax.swing.JTextArea;

import org.snmp4j.PDU;
import org.snmp4j.smi.VariableBinding;

public class OidInfo {
	private String Oid;
	private boolean isRepeatable;
	private int pduType;
	private VariableBinding binding;
	private ParamType paramType;
	
	private Object initValue;
	private Object prevValue;
	private Object currValue;
	
	private JTextArea txtControl;
	
	/*public OidInfo() {
		Oid = "";
		isRepeatable = false;
		pduType = PDU.GET;
		binding = null;
		paramType = ParamType.STRING;
		initValue = "";
		prevValue = "";
		currValue = "";
	}*/

	public OidInfo(String oid, boolean isRepeatable, int pduType, ParamType paramType, JTextArea txt) {
		super();
		Oid = oid;
		this.isRepeatable = isRepeatable;
		this.pduType = pduType;
		this.binding = null;
		this.paramType = paramType;
		
		initValue = null;
		prevValue = null;
		currValue = null;
		txtControl = txt;
	}
	
	public OidInfo(String oid, boolean isRepeatable, ParamType paramType, JTextArea txt) {
		this(oid, isRepeatable, PDU.GET, paramType, txt);
	}
	
	public OidInfo(String oid, boolean isRepeatable, ParamType paramType) {
		this(oid, isRepeatable, PDU.GET, paramType, null);
	}
	
	public OidInfo(String oid, ParamType paramType, JTextArea txt) {
		this(oid, false, PDU.GET, paramType, txt);
	}
	
	public OidInfo(String oid, ParamType paramType) {
		this(oid, false, PDU.GET, paramType, null);
	}
	
	public void setValue(VariableBinding newVariable) {
		Object newValue = null;
		
		if (pduType == PDU.GETNEXT) {
			binding = newVariable;
		}
		
		switch (paramType) {
			case DOUBLE:
				String temp = newVariable.getVariable().toString();
				newValue = Double.parseDouble(temp);
				break;
			case INT:
				newValue = newVariable.getVariable().toInt();
				break;
			case LONG:
				newValue = newVariable.getVariable().toLong();
				break;
			case STRING:
				newValue = newVariable.getVariable().toString();
				break;
		}
		
		if ((newValue != null) && (txtControl != null)) {
			String s = newValue.toString();
			if (s.length() == 0) {
				s = "<empty>";
			}
			txtControl.setText(s);
		}
		
		if (initValue == null) {
			initValue = newValue;
		}
		
		prevValue = currValue;
		currValue = newValue;
	}
	
	public Object getValue() {
		Object res = currValue;
		if (res == null) {
			switch (paramType) {
			case STRING:
				res = "";
				break;
			case LONG:
				res = (long)0;
				break;
			case INT:
				res = 0;
				break;
			case DOUBLE:
				res = 0.0;
				break;
			}
		}
		return res;
	}
	
	public Object getCurrValue() {
		
		if ((prevValue == null) || (currValue == null)) {
			return getValue();
		}
		
		switch (paramType) {
			case STRING:
				return getValue();
			case DOUBLE:
				return (Double)currValue - (Double)prevValue;
			case INT:
				return (Integer)currValue - (Integer)prevValue;
			case LONG:
				long val = (Long)currValue - (Long)prevValue;
				if (val < 0) {
					long diff = Integer.MAX_VALUE - (Long)prevValue;
					val = diff - val;
				}
				return val;
		}
		
		return getValue();
	}
	
	public Object getAvgValue() {
		
		if ((initValue == null) || (currValue == null)) {
			return getValue();
		}
		
		switch (paramType) {
			case STRING:
				return getValue();
			case DOUBLE:
				return (Double)currValue - (Double)initValue;
			case INT:
				return (Integer)currValue - (Integer)initValue;
			case LONG:
				long val = (Long)currValue - (Long)initValue;
				if (val < 0) {
					long diff = Integer.MAX_VALUE - (Long)initValue;
					val = diff - val;
				}
				return val;
		}
		
		return getValue();
	}

	public String getOid() {
		return Oid;
	}

	public void setOid(String oid) {
		Oid = oid;
	}

	public boolean isRepeatable() {
		return isRepeatable;
	}

	public void setRepeatable(boolean isRepeatable) {
		this.isRepeatable = isRepeatable;
	}

	public int getPduType() {
		return pduType;
	}

	public void setPduType(int pduType) {
		this.pduType = pduType;
	}

	public VariableBinding getBinding() {
		return binding;
	}

	public void setBinding(VariableBinding binding) {
		this.binding = binding;
	}

	public ParamType getParamType() {
		return paramType;
	}

	public void setParamType(ParamType paramType) {
		this.paramType = paramType;
	}

	public JTextArea getTxtControl() {
		return txtControl;
	}
	
}
